import config from './config/httpConfig.js';
import store from '../store/index.js'
var request = (option, is_loading = true) => {
	var params = option.params;
	var token = uni.getStorageSync('token');
	var url = (config.IS_ONLINE ? config.DOMAIN : config.DEV_DOMAIN) + option.url;
	let ClientType = ''
	//#ifdef H5
	ClientType = 'wap'
	//#endif
	//#ifdef APP-PLUS
	ClientType = 'app'
	//#endif
	//#ifdef MP-WEIXIN
	ClientType = 'wxapp'
	//#endif
	if (is_loading) {
		uni.showLoading({
			title: '加载中...',
			mask:true,
		})
	}
	// 判断是是否有token
	uni.request({
		url: url,
		data: params,
		method: option.method || "POST",
		header: option.header || {
			"appid": config.APPID,
			"Authorization": token,
			"ClientType": ClientType
		},
		dataType: "json",
		responseType: "text",
		success: res => {
			option.success(res.data);
			if (is_loading) uni.hideLoading()
			let routes = getCurrentPages()
			let curRoute = routes[routes.length - 1].route
			if ((res.data.code === 401) && curRoute!== "/pages/publics/login") {
				console.log("未登录")
				// uni.clearStorageSync()
				// uni.showModal({
				// 	title:"提示",
				// 	content:res.data.message,
				// 	confirmColor: '#108EE9',
				// 	showCancel: false,
				// 	success:(res)=> {
				// 		uni.navigateTo({
				// 			url:'/pages/publics/guide'
				// 		})
				// 	}
				// })
				// uni.navigateTo({
				// 	url:'/pages/publics/guide'
				// })
			}else if(res.data.code == 101){
				uni.showModal({
					title:"提示",
					content:res.data.message,
					confirmColor: '#108EE9',
					showCancel: false,
					success:(res)=> {
						
					}
				})
			}
		},
		fail: res => {
			if (is_loading) uni.hideLoading()
			throw new Error(res);
		},
		complete: () => {
			if (is_loading) uni.hideLoading()
		}
	})
}
//get请求
var get = (option, is_loading = true) => {
	var params = option.params;
	var token = uni.getStorageSync('token');
	var url = (config.IS_ONLINE ? config.DOMAIN : config.DEV_DOMAIN) + option.url;
	let ClientType = ''
	//#ifdef H5
	ClientType = 'wap'
	//#endif
	//#ifdef APP-PLUS
	ClientType = 'app'
	//#endif
	//#ifdef MP-WEIXIN
	ClientType = 'wxapp'
	//#endif
	if (is_loading) {
		uni.showLoading({
			title: '加载中...'
		})
	}
	// 判断是是否有token
	uni.request({
		url: url,
		data: params,
		method: option.method || "get",
		header: option.header || {
			"appid": config.APPID,
			"Authorization": token,
			"ClientType": ClientType
		},
		dataType: "json",
		responseType: "text",
		success: res => {
			option.success(res.data);
			
			if (is_loading) uni.hideLoading()
			let routes = getCurrentPages()
			let curRoute = routes[routes.length - 1].route
			if ((res.data.code === 401) && curRoute!== "/pages/publics/login") {
				// uni.showModal({
				// 	title:"提示",
				// 	content:res.data.message,
				// 	confirmColor: '#108EE9',
				// 	showCancel: true,
				// 	success:(res)=> {
				// 		console.log(res)
				// 		if(res.confirm){
				// 			uni.navigateTo({
				// 				url:'/pages/publics/guide',
				// 				fail (error) {
				// 					console.log(error)
				// 				}
								
				// 			})
				// 		}
						
				// 	}
				// })
				// uni.navigateTo({
				// 	url:'/pages/publics/guide',
				// 	fail (error) {
				// 		console.log(error)
				// 	}
					
				// })
				// uni.clearStorageSync()
				
			}
		},
		fail: res => {
			if (is_loading) uni.hideLoading()
			throw new Error(res);
		},
		complete: () => {
			if (is_loading) uni.hideLoading()
		}
	})
}


var login = (option) => {
	uni.login({
		success: res => {
			var params = {
				code: res.code
			}
			request({
				that: option.that,
				c: config.LOGIN_ACTION.c,
				a: config.LOGIN_ACTION.a,
				params: params,
				success: result => {
					if (result.data.code === config.SUCCESS_CODE) {
						option.that.$store.commit("upToken", result.data.data.token);
						request(option);
					} else {
						uni.showModal({
							title: "提示",
							content: result.data.msg
						});
						return false;
					}
				}
			})
		}
	})
}
//上传图片
var handlerUploadPic = (backcall) => {
	uni.chooseImage({
		count: 9,
		sizeType: ["original", "compressed"],
		sourceType: ["album", "camera"],
		success: res => {
			var tempFilePaths = res.tempFilePaths;
			console.log("上传照片",res)
			tempFilePaths.forEach((item,index)=>{
				
				uploadfile({
					tempFilePath: item,
					success: res => {
						res.tempNameArray = tempFilePaths //记录顺序
						backcall(res);
					}
				})
			})
			// uploadfile({
			// 	tempFilePath: tempFilePaths[0],
			// 	success: res => {
			// 		backcall(res);
			// 	}
			// })
		}
	})
}
//上传图片视频
var handlerUploadVideo = (backcall) => {
	uni.chooseMedia({
		sourceType: [
			'album', 'camera'
		],
		count:1,
		mediaType:['video'],
		compressed: true,
		maxDuration: 60,
		camera: 'back',
		success: result => {
			var tempFilePath = result.tempFiles[0].tempFilePath;
			console.log("上传视频",result)
			uploadfile({
				tempFilePath: tempFilePath,
				success: res => {
					backcall(res);
					console.log("打印视频封面,并且上传",result.tempFiles[0].thumbTempFilePath,res.data.id)
					//上传封面，需要传id
					uni.uploadFile({
						url: config.VIDEO_BG_UPLOAD_URL,
						filePath: result.tempFiles[0].thumbTempFilePath,
						header: {
							"Content-Type": "multipart/form-data"
						},
						formData:{
							fileId:res.data.id
						},
						name: "file",
						success: res_img => {
							var imgData = JSON.parse(res_img.data);
							console.log("上传封面成",imgData)
							
						}
					})
				}
			})
			
		}
	})
}

var uploadfile = (option) => {
	uni.showLoading({
		title: "上传中..."
	});
	var url = config.IS_ONLINE ? config.UPLOAD_URL : config.DEV_UPLOAD_URL;
	uni.uploadFile({
		url: url,
		filePath: option.tempFilePath,
		header: {
			"appid": config.APPID
		},
		formData:{
			// fileId:option.
		},
		name: "file",
		success: result => {
			uni.hideLoading();
			var imgData = JSON.parse(result.data);
			if (imgData.code === config.SUCCESS_CODE) {
				option.success(imgData);
			} else {
				uni.showToast({
					title: '上传失败',
					icon: 'none'
				})
				option.success(false);
			}
		}
	})
}
//上传微信文件
var handlerUploadWx = (backcall) => {
	wx.chooseMessageFile({
	  count: 10,
	  type: 'file',
	  success (res) {
	    // tempFilePath可以作为img标签的src属性显示图片
	    const tempFilePath = res.tempFiles
		console.log(res)
		uploadfiles({
			tempFilePath: tempFilePath[0].path,
			success: res => {
				backcall(res);
			}
		})
	  }
	})
}
//微信上传文
var uploadfiles = (option) => {
	uni.showLoading({
		title: "上传中..."
	});
	var url = config.IS_ONLINE ? config.UPLOAD_URL : config.DEV_UPLOAD_URL;
	uni.uploadFile({
		url: url,
		filePath: option.tempFilePath,
		header: {
			"appid": config.APPID,
			"Authorization": uni.getStorageSync('token'),
		},
		name: "file",
		success: result => {
			uni.hideLoading();
			var imgData = JSON.parse(result.data);
			if (imgData.code === config.SUCCESS_CODE) {
				option.success(imgData);
				uni.showToast({
					title: '上传成功',
					icon: 'none'
				})
			} else {
				uni.showToast({
					title: '上传失败',
					icon: 'none'
				})
				option.success(false);
			}
		}
	})
}
module.exports = {
	request,
	get,
	handlerUploadPic,
	handlerUploadVideo,
	handlerUploadWx
}
